/* 
 * File:   LEDDisplay.h
 * Author: root
 *
 * Created on December 23, 2008, 3:48 PM
 */

#ifndef H_RCGD_LEDDISPLAY_H
#define H_RCGD_LEDDISPLAY_H

#define PIN_LED_OE		6+PA_INDEX	// LED/OE
#define PIN_LED_DIN	  	16+PB_INDEX	// SER
#define PIN_LED_CLK	  	24+PB_INDEX	// SR_CLK
#define PIN_LED_LATCH  	23+PB_INDEX	// R_CLK

#define PIN_P12V_S		3+PA_INDEX	// P12V_S
#define PIN_N12V_S		4+PA_INDEX	// N12V_S
#define PIN_P5V_S		5+PA_INDEX	// P5V_S


//#define LEDDebug(x)    printf(x)
#define LEDDebug(x)    

#define SetLedOeHigh(x)  	do{ LEDDebug("oe-high ");ioctl( x, IOCTL_GPIO_OUT_SET, PIN_LED_OE ); }while(0)
#define SetLedOeLow(x)  	do{ LEDDebug("oe-low ");ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_LED_OE ); }while(0)

#define SetLedDinHigh(x)  	do{ LEDDebug("din-high ");ioctl( x, IOCTL_GPIO_OUT_SET, PIN_LED_DIN ); }while(0)
#define SetLedDinLow(x)  	do{ LEDDebug("din-low ");ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_LED_DIN ); }while(0)

#define SetLedClkHigh(x)  	do{ LEDDebug("clk-high ");ioctl( x, IOCTL_GPIO_OUT_SET, PIN_LED_CLK ); }while(0)
#define SetLedClkLow(x)  	do{ LEDDebug("clk-low ");ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_LED_CLK ); }while(0)

#define SetLedLatchHigh(x)  do{ LEDDebug("lat-high ");ioctl( x, IOCTL_GPIO_OUT_SET, PIN_LED_LATCH ); }while(0)
#define SetLedLatchLow(x)  	do{ LEDDebug("lat-low ");ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_LED_LATCH ); }while(0)

#define	SetLEDPort(x)	do{ ioctl( x, IOCTL_GPIO_DIR_OUT, PIN_LED_OE ); ioctl( x, IOCTL_GPIO_DIR_OUT, PIN_LED_DIN ); ioctl( x, IOCTL_GPIO_DIR_OUT, PIN_LED_CLK ); ioctl( x, IOCTL_GPIO_DIR_OUT, PIN_LED_LATCH );}while(0)


//#define SelectP5VtoSense();	do{ iLEDgpndu &= (~0xE0); iLEDgpndu |= wbD7; }while(0);
//#define SelectN12VtoSense();	do{ iLEDgpndu &= (~0xE0); iLEDgpndu |= wbD6; }while(0);
//#define SelectP12VtoSense();	do{ iLEDgpndu &= (~0xE0); iLEDgpndu |= wbD5; }while(0);


#define SetP8VHigh(x)  		do{ ioctl( x, IOCTL_GPIO_OUT_SET, PIN_P12V_S ); }while(0)  
#define SetP8VLow(x)  		do{ ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_P12V_S ); }while(0)   

#define SetN8VHigh(x)  		do{ ioctl( x, IOCTL_GPIO_OUT_SET, PIN_N12V_S ); }while(0)  
#define SetN8VLow(x)  		do{ ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_N12V_S ); }while(0)

#define SetP5VHigh(x)  		do{ ioctl( x, IOCTL_GPIO_OUT_SET, PIN_P5V_S ); }while(0)  
#define SetP5VLow(x)  		do{ ioctl( x, IOCTL_GPIO_OUT_CLR, PIN_P5V_S ); }while(0)      

//#define SelectP12VtoSense(x)	do{ SetP12VHigh(x); SetN12VLow(x);  SetP5VLow(x);  }while(0)
//#define SelectN12VtoSense(x)	do{ SetP12VLow(x);  SetN12VHigh(x); SetP5VLow(x);  }while(0)
//#define SelectP5VtoSense(x)		do{ SetP12VLow(x);  SetN12VLow(x);  SetP5VHigh(x); }while(0)

#define SelectP5VtoSense(x)		do{ SetP8VLow(x);  SetN8VLow(x);  SetP5VHigh(x); }while(0)
#define SelectP8VtoSense(x)		do{ SetN8VLow(x);  SetP5VLow(x);  SetP8VHigh(x); }while(0)
#define SelectN8VtoSense(x)		do{ SetP8VLow(x);  SetP5VLow(x);  SetN8VHigh(x); }while(0)
#define DeSelectSense(x)		do{ SetP8VLow(x);  SetP5VLow(x);  SetN8VLow(x); }while(0)


extern int iLEDgpndu;
extern int iLEDfd;

//functions called by S/W
//extern void InitDisplayLEDgpndu(int fd);
extern void DisplayLEDgpndu();

#define SyncLEDDisplay()	do{ led_blk_cnt=0; }while(0)

//#define BLK_PATTERN 0x01F // 00_0001_1111
//#define ON_PATTERN  0x3FF // 11_1111_1111
//#define OFF_PATTERN 0x000 // 00_0000_0000

//#define BLK_PATTERN1 0x0101 // 0000_0001_0000_0001
//#define BLK_PATTERN2 0x1111 // 0001_0001_0001_0001
//#define BLK_PATTERN3 0x0303 // 0000_0011_0000_0011
//#define BLK_PATTERN4 0x3333 // 0011_0011_0011_0011
//#define BLK_PATTERN5 0x5555 // 0101_0101_0101_0101
//#define BLK_PATTERN6 0x0003 // 0000_0000_0000_0011

#define BLK_PP1S0R2  0x021 // 00_0010_0001
#define BLK_PP1S0R4  0x063 // 00_0110_0011
#define BLK_PP2S0R2  0x001 // 00_0000_0001
#define BLK_PP2S0R4  0x003 // 00_0000_0011


#define ON_PATTERN  0xFFFF // 1111_1111_1111_1111
#define OFF_PATTERN 0x0000 // 0000_0000_0000_0000

extern int led_blk_cnt;
extern int led_blk[8];

//#define LedOnGPNDU(x)    iLEDgpndu |=   (0x01<<((x)-1)) //iLEDgpndu = iLEDgpndu | (0x01<<((x)-1))
//#define LedOffGPNDU(x)   iLEDgpndu &=  ~(0x01<<((x)-1)) //iLEDgpndu = iLEDgpndu | (0x01<<((x)-1))
#define LedSetGDU(x,v)    led_blk[x] = v;

#define led_run_red_on()	do{ LedSetGDU(6,ON_PATTERN); LedSetGDU(7,OFF_PATTERN); }while(0)
#define led_run_red_off()	do{ LedSetGDU(6,OFF_PATTERN);LedSetGDU(7,OFF_PATTERN);} while(0)
#define led_run_red_blk1()	do{ LedSetGDU(6,BLK_PP1S0R2);LedSetGDU(7,OFF_PATTERN); }while(0)
#define led_run_red_blk2()	do{ LedSetGDU(6,BLK_PP1S0R4);LedSetGDU(7,OFF_PATTERN); }while(0)
#define led_run_red_blk3()	do{ LedSetGDU(6,BLK_PP2S0R2);LedSetGDU(7,OFF_PATTERN); }while(0)
#define led_run_red_blk4()	do{ LedSetGDU(6,BLK_PP2S0R4);LedSetGDU(7,OFF_PATTERN); }while(0)

#define led_run_grn_on()	do{ LedSetGDU(7,ON_PATTERN); LedSetGDU(6,OFF_PATTERN); }while(0)
#define led_run_grn_off()	do{ LedSetGDU(7,OFF_PATTERN);LedSetGDU(6,OFF_PATTERN); }while(0)
#define led_run_grn_blk1()	do{ LedSetGDU(7,BLK_PP1S0R2);LedSetGDU(6,OFF_PATTERN); }while(0)
#define led_run_grn_blk2()	do{ LedSetGDU(7,BLK_PP1S0R4);LedSetGDU(6,OFF_PATTERN); }while(0)
#define led_run_grn_blk3()	do{ LedSetGDU(7,BLK_PP2S0R2);LedSetGDU(6,OFF_PATTERN); }while(0)
#define led_run_grn_blk4()	do{ LedSetGDU(7,BLK_PP2S0R4);LedSetGDU(6,OFF_PATTERN); }while(0)




#define led_clk_red_on()   do{ LedSetGDU(2,ON_PATTERN); LedSetGDU(3,OFF_PATTERN); }while(0)
#define led_clk_red_off()  do{ LedSetGDU(2,OFF_PATTERN);LedSetGDU(3,OFF_PATTERN);} while(0)
#define led_clk_red_blk1()  do{ LedSetGDU(2,BLK_PP1S0R2);LedSetGDU(3,OFF_PATTERN); }while(0)
#define led_clk_red_blk2()  do{ LedSetGDU(2,BLK_PP1S0R4);LedSetGDU(3,OFF_PATTERN); }while(0)
#define led_clk_red_blk3()  do{ LedSetGDU(2,BLK_PP2S0R2);LedSetGDU(3,OFF_PATTERN); }while(0)
#define led_clk_red_blk4()  do{ LedSetGDU(2,BLK_PP2S0R4);LedSetGDU(3,OFF_PATTERN); }while(0)



#define led_clk_grn_on()    do{ LedSetGDU(3,ON_PATTERN); LedSetGDU(2,OFF_PATTERN); }while(0)
#define led_clk_grn_off()   do{ LedSetGDU(3,OFF_PATTERN);LedSetGDU(2,OFF_PATTERN); }while(0)
#define led_clk_grn_blk1()   do{ LedSetGDU(3,BLK_PP1S0R2);LedSetGDU(2,OFF_PATTERN); }while(0)
#define led_clk_grn_blk2()   do{ LedSetGDU(3,BLK_PP1S0R4);LedSetGDU(2,OFF_PATTERN); }while(0)
#define led_clk_grn_blk3()   do{ LedSetGDU(3,BLK_PP2S0R2);LedSetGDU(2,OFF_PATTERN); }while(0)
#define led_clk_grn_blk4()   do{ LedSetGDU(3,BLK_PP2S0R4);LedSetGDU(2,OFF_PATTERN); }while(0)




//#define led_act_red_on()    do{ LedSetGDU(4,ON_PATTERN); LedSetGDU(5,OFF_PATTERN); }while(0)
//#define led_act_red_off()   do{ LedSetGDU(4,OFF_PATTERN);LedSetGDU(5,OFF_PATTERN);} while(0)
//#define led_act_red_blk1()   do{ LedSetGDU(4,BLK_PATTERN1);LedSetGDU(5,OFF_PATTERN); }while(0)
//#define led_act_red_blk2()   do{ LedSetGDU(4,BLK_PATTERN2);LedSetGDU(5,OFF_PATTERN); }while(0)
//#define led_act_red_blk3()   do{ LedSetGDU(4,BLK_PATTERN3);LedSetGDU(5,OFF_PATTERN); }while(0)
//#define led_act_red_blk4()   do{ LedSetGDU(4,BLK_PATTERN4);LedSetGDU(5,OFF_PATTERN); }while(0)
//#define led_act_red_blk5()   do{ LedSetGDU(4,BLK_PATTERN5);LedSetGDU(5,OFF_PATTERN); }while(0)
//#define led_act_red_blk6()   do{ LedSetGDU(4,BLK_PATTERN6);LedSetGDU(5,OFF_PATTERN); }while(0)

//#define led_act_grn_on()    do{ LedSetGDU(5,ON_PATTERN); LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_off()   do{ LedSetGDU(5,OFF_PATTERN);LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_blk1()   do{ LedSetGDU(5,BLK_PATTERN1);LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_blk2()   do{ LedSetGDU(5,BLK_PATTERN2);LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_blk3()   do{ LedSetGDU(5,BLK_PATTERN3);LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_blk4()   do{ LedSetGDU(5,BLK_PATTERN4);LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_blk5()   do{ LedSetGDU(5,BLK_PATTERN5);LedSetGDU(4,OFF_PATTERN); }while(0)
//#define led_act_grn_blk6()   do{ LedSetGDU(5,BLK_PATTERN6);LedSetGDU(4,OFF_PATTERN); }while(0)


#define led_act_red_on()
#define led_act_red_off()
#define led_act_red_blk1()
#define led_act_red_blk2()
#define led_act_red_blk3()
#define led_act_red_blk4()
#define led_act_red_blk5()
#define led_act_red_blk6()

#define led_act_grn_on()
#define led_act_grn_off()
#define led_act_grn_blk1()
#define led_act_grn_blk2()
#define led_act_grn_blk3()
#define led_act_grn_blk4()
#define led_act_grn_blk5()
#define led_act_grn_blk6()

#define led_gps_red_on()    do{ LedSetGDU(4,ON_PATTERN); LedSetGDU(5,OFF_PATTERN); }while(0)
#define led_gps_red_off()   do{ LedSetGDU(4,OFF_PATTERN);LedSetGDU(5,OFF_PATTERN);} while(0)
#define led_gps_red_blk1()   do{ LedSetGDU(4,BLK_PP1S0R2);LedSetGDU(5,OFF_PATTERN); }while(0)
#define led_gps_red_blk2()   do{ LedSetGDU(4,BLK_PP1S0R4);LedSetGDU(5,OFF_PATTERN); }while(0)
#define led_gps_red_blk3()   do{ LedSetGDU(4,BLK_PP2S0R2);LedSetGDU(5,OFF_PATTERN); }while(0)
#define led_gps_red_blk4()   do{ LedSetGDU(4,BLK_PP2S0R4);LedSetGDU(5,OFF_PATTERN); }while(0)



#define led_gps_grn_on()    do{ LedSetGDU(5,ON_PATTERN); LedSetGDU(4,OFF_PATTERN); }while(0)
#define led_gps_grn_off()   do{ LedSetGDU(5,OFF_PATTERN);LedSetGDU(4,OFF_PATTERN); }while(0)
#define led_gps_grn_blk1()   do{ LedSetGDU(5,BLK_PP1S0R2);LedSetGDU(4,OFF_PATTERN); }while(0)
#define led_gps_grn_blk2()   do{ LedSetGDU(5,BLK_PP1S0R4);LedSetGDU(4,OFF_PATTERN); }while(0)
#define led_gps_grn_blk3()   do{ LedSetGDU(5,BLK_PP2S0R2);LedSetGDU(4,OFF_PATTERN); }while(0)
#define led_gps_grn_blk4()   do{ LedSetGDU(5,BLK_PP2S0R4);LedSetGDU(4,OFF_PATTERN); }while(0)




//#define led_gps_red_on()    do{ LedSetGDU(0,ON_PATTERN); LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_off()   do{ LedSetGDU(0,OFF_PATTERN);LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_blk1()   do{ LedSetGDU(0,BLK_PATTERN1);LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_blk2()   do{ LedSetGDU(0,BLK_PATTERN2);LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_blk3()   do{ LedSetGDU(0,BLK_PATTERN3);LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_blk4()   do{ LedSetGDU(0,BLK_PATTERN4);LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_blk5()   do{ LedSetGDU(0,BLK_PATTERN5);LedSetGDU(1,OFF_PATTERN); }while(0)
//#define led_gps_red_blk6()   do{ LedSetGDU(0,BLK_PATTERN6);LedSetGDU(1,OFF_PATTERN); }while(0)
//
//#define led_gps_grn_on()    do{ LedSetGDU(1,ON_PATTERN); LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_off()   do{ LedSetGDU(1,OFF_PATTERN);LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_blk1()   do{ LedSetGDU(1,BLK_PATTERN1);LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_blk2()   do{ LedSetGDU(1,BLK_PATTERN2);LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_blk3()   do{ LedSetGDU(1,BLK_PATTERN3);LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_blk4()   do{ LedSetGDU(1,BLK_PATTERN4);LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_blk5()   do{ LedSetGDU(1,BLK_PATTERN5);LedSetGDU(0,OFF_PATTERN); }while(0)
//#define led_gps_grn_blk6()   do{ LedSetGDU(1,BLK_PATTERN6);LedSetGDU(0,OFF_PATTERN); }while(0)


#define led_ant_red_on()    do{ LedSetGDU(0,ON_PATTERN); LedSetGDU(1,OFF_PATTERN); }while(0)
#define led_ant_red_off()   do{ LedSetGDU(0,OFF_PATTERN);LedSetGDU(1,OFF_PATTERN); }while(0)
#define led_ant_red_blk1()   do{ LedSetGDU(0,BLK_PP1S0R2);LedSetGDU(1,OFF_PATTERN); }while(0)
#define led_ant_red_blk2()   do{ LedSetGDU(0,BLK_PP1S0R4);LedSetGDU(1,OFF_PATTERN); }while(0)
#define led_ant_red_blk3()   do{ LedSetGDU(0,BLK_PP2S0R2);LedSetGDU(1,OFF_PATTERN); }while(0)
#define led_ant_red_blk4()   do{ LedSetGDU(0,BLK_PP2S0R4);LedSetGDU(1,OFF_PATTERN); }while(0)

#define led_ant_grn_on()    do{ LedSetGDU(1,ON_PATTERN); LedSetGDU(0,OFF_PATTERN); }while(0)
#define led_ant_grn_off()   do{ LedSetGDU(1,OFF_PATTERN);LedSetGDU(0,OFF_PATTERN); }while(0)
#define led_ant_grn_blk1()   do{ LedSetGDU(1,BLK_PP1S0R2);LedSetGDU(0,OFF_PATTERN); }while(0)
#define led_ant_grn_blk2()   do{ LedSetGDU(1,BLK_PP1S0R4);LedSetGDU(0,OFF_PATTERN); }while(0)
#define led_ant_grn_blk3()   do{ LedSetGDU(1,BLK_PP2S0R2);LedSetGDU(0,OFF_PATTERN); }while(0)
#define led_ant_grn_blk4()   do{ LedSetGDU(1,BLK_PP2S0R4);LedSetGDU(0,OFF_PATTERN); }while(0)





#ifdef	__cplusplus
}
#endif

#endif	/* _LEDDISPLAY_H */

